﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public enum CSMTableHints : byte
    {
        Thin = 0,
        Medium = 1,
        Thick = 2
    }
    
    public class DefineFontAlignZonesTag : Tag, IReferenceCharacterId
    {
        private readonly IDictionaryIdQueryable _querySource;

        public ushort FontID
        {
            get;
            protected set;
        }

        public CSMTableHints CSMTableHint
        {
            get;
            protected set;
        }

        public ZoneRecord[] ZoneTable
        {
            get;
            protected set;
        }

        public DefineFontAlignZonesTag(int len, int offset, IDictionaryIdQueryable querySource)
            : base(TagCodes.DefineFontAlignZones, len, offset)
        {
            _querySource = querySource;
        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            FontID = reader.ReadUInt16();

            CSMTableHint = (CSMTableHints)reader.ReadUInt32(2);
            
            //Reserved
            reader.SkipBits(6);

            reader.AlignBits();

            Tag tag;
            DefineFontTag fontTag;

            if (_querySource.TryGetValue(FontID, out tag) == false)
            {
                throw new SerializationException("DefineFontAlignZonesTag Tag referring to unknown Font ID");
            }
            
            if ((fontTag = tag as DefineFontTag) == null)
            {
                throw new SerializationException("ID exists but doesn't belong to a font Tag");
            }

            ZoneTable = new ZoneRecord[fontTag.NumGylphs];

            for (int i = 0; i < fontTag.NumGylphs; i++)
            {
                ZoneTable[i] = new ZoneRecord();
                ZoneTable[i].SwfDeserialize(reader);
            }
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        public bool IsReferencing
        {
            get { throw new NotImplementedException(); }
        }

        public ushort ReferencedId
        {
            get { return FontID; }
        }

        public IEnumerable<Type> ReferencedAttributes
        {
            get { throw new NotImplementedException(); }
        }

        public Type ReferencedType
        {
            get { throw new NotImplementedException(); }
        }
    }
}
